<?php
require_once FOREGROUND_COMMON_ACTION_SERVICE_DIR . '/ajax.php';

class foreground_member_mobile_do_register_action_service extends foreground_ajax_common_action_service
{
    protected static $_instance;

    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }


    protected function _current_page_process()
    {	     		     
	    $result = array('status' => 0, 'message' => '', 'data' => array());
	     
	    //var_dump($_POST); exit();
	    $mobile = init_user_data_key($_POST, 'mobile', 'str');
	    $password = init_user_data_key($_POST, 'password', 'str');
	    $confirm_password = init_user_data_key($_POST, 'confirm_password', 'str');
	    $member_register_captcha = init_user_data_key($_POST, 'member_register_captcha', 'str');
	
        if(empty($mobile)){
            $result['status']  = -1;
            $result['message'] = '请输入手机';
        }else{
            $pattern_mobile = '/^\d{11}$/';
            $match_count    = preg_match($pattern_mobile, $mobile);
            if($match_count < 1){
                $result['status']  = -2;
                $result['message'] = '手机格式错误';
            }
        }

        if($result['status'] == 0){
            if(empty($password)){
                $result['status']  = -3;
                $result['message'] = '请输入密码';
            }elseif(empty($confirm_password)){
                $result['status']  = -4;
                $result['message'] = '请输入确认密码';
            }elseif($password != $confirm_password){
                $result['status']  = -5;
                $result['message'] = '密码不一致';
            }
        }

        if($result['status'] == 0){
            if(empty($member_register_captcha)){
                $result['status']  = -6;
                $result['message'] = '请输入验证码';
            }else{
                $action_data = wwy_action::$data;
                $session_member_register_captcha = $action_data['session_member_register_captcha'];
                //var_dump($session_member_register_captcha); exit();
	            if(empty($session_member_register_captcha)){
	                $result['status']  = -400;
	                $result['message'] = '非法操作';
	            }else{
    	            if(strtolower($member_register_captcha) !== $session_member_register_captcha){
    	                $result['status']  = -400;
    	                $result['message'] = '验证码错误';
    	            }
	            }
            }
        }
        //var_dump($result); exit();
        //exit();

        if($result['status'] == 0){
            $model_foreground_member = wwy_resource_container::get_model_foreground_member();
            //var_dump($model_foreground_member); exit();
            $where   = array('mobile' => $mobile);
            $member  = $model_foreground_member->fetch_row($where);
            //var_dump($member); exit();
            if(!empty($member)){
                $result['status']  = -8;
                $result['message'] = '该手机已被注册';
            }else{
                $write_db = $this->_get_write_db();
                //var_dump($write_db); exit();
                $pdo = $write_db->get_connection();
                //var_dump($pdo); exit();
                $pdo->beginTransaction();

                try{
                    $time = time();
                    $password = md5($password);                 
                    
                    $member_info = array(
                        'mobile'         => $mobile,
                        'password'       => $password,
                        'create_time'    => $time,
                        'update_time'    => $time,
                        'status'         => 1,
                    );
                    $add_result = $model_foreground_member->mobile_add($member_info);
                    //var_dump($add_result); exit();
                    if(!$add_result){
                        $result['status']  = -9;
                        $result['message'] = '注册失败';
                    }else{                       
                        $result['status']  = 1;
                        $result['message'] = '注册成功，请登录';
                        
                        $pdo->commit();	                        	                        	                        	                        
                    }
                     
                }catch (Exception $e){
                    $pdo->rollBack();

                    throw $e;
                }
            }
        }
	     
	    self::set_view_status($result['status']);
	    self::set_view_message($result['message']);
    }
}